Dart, a prominent programming language, is developed and maintained by tech titan Google. Google’s Flutter leverages Dart as its language, empowering developers to develop cross-platform applications from a single codebase.
It aims to provide a productive and readable C-style syntax while offering features that make it easier for developers to build high-performance applications.
In this article, you will learn how to install Dart on Linux and configure Vim as an IDE for Dart, with some examples showcasing the usage.
Tutorial Details
Description | Dart Programming Language |
Difficulty Level | Low |
Root or Sudo Privileges | Yes |
OS Compatibility | Debian, Ubuntu, Linux Mint, etc. |
Prerequisites | – |
Internet Required | Yes |
How to Install Dart on Linux
Please note that Dart installation is currently supported on Debian and Ubuntu-based distributions only, with no official support for Red Hat, Fedora, or Arch-based distributions.
You can use the Flutter snap package on unsupported distributions since Dart is included, but the decision is yours.
Now, Installing Dart in Debian or Ubuntu-based distributions is a breeze – just open your terminal and execute the following commands one after the other.
1. Begin by refreshing your system’s repository information to ensure it’s up-to-date, then proceed to upgrade any pending packages.
$ sudo apt update
$ sudo apt upgrade
2. Add the GPG key from Google.
$ wget -qO- https://dl-ssl.google.com/linux/linux_signing_key.pub | sudo gpg --dearmor -o /usr/share/keyrings/dart.gpg
3. Add the Dart SDK repository.
$ echo 'deb [signed-by=/usr/share/keyrings/dart.gpg arch=amd64] https://storage.googleapis.com/download.dartlang.org/linux/debian stable main' | sudo tee /etc/apt/sources.list.d/dart_stable.list
4. Finally, update your system repository information and install the latest version of Dart on your system.
$ sudo apt update
$ sudo apt install dart
Once the installation is finished, run the following command to ensure accessibility to Dart:
$ dart --version
Output:
Disable Sending Usage Metrics and Crash Reports to Google
Dart is a Google product and may send anonymous usage metrics and crash reports to help improve its product and overall development experience.
While it has no impact on functionality, individuals prioritizing their privacy can easily disable anonymous analytics in Dart by setting the “--disable-analytics
” flag.
For Individual Commands
When running a Dart command, you can add the “--disable-analytics
” flag to disable the analytics for that specific command. For example:
$ dart --disable-analytics your_script.dart
For All Commands
To disable analytics for all Dart commands, you can set the environment variables in your shell configuration files (let’s say “~/.bashrc
“).
1. Open your shell configuration file using your choice of text editor:
$ vim ~/.bashrc
2. Add the following line at the end of the file:
alias dart="dart --disable-analytics"
3. Save the file, close it, and then refresh your shell configuration.
$ source ~/.bashrc
Now, let’s delve into practical examples showcasing the use of Dart.
How to Use Dart on Linux
Assuming you’ve already completed the earlier steps and installed the latest Dart version on your system. Once you have the SDK installed, you can use the “dart
” command-line tool to run your Dart programs.
Here’s how you can do it:
1. Create a file using your preferred text editor, then copy the following content into it:
void main() {
print('Hello World!');
}
2. Save the file with the “.dart
” extension as the “dart_exp1.dart
” filename in a suitable location.
3. Open your terminal and navigate to the directory where your Dart program is located. To run the program, enter the “dart
” command and the filename of your Dart file (assuming “dart_exp1.dart
“).
$ dart dart_exp1.dart
Output:
Congratulations on running your first Dart program on Linux!
Now, you can go further and start writing complex Dart programs that include multiple variables and data types.
For example, the following program will utilize multiple variables with different data types and then output them together using the “print()
” function.
void main() {
// Variables and data types
int age = 18;
double height = 6.2;
String name = 'Alex';
bool isStudent = true;
print('$name is $age years old, $height feet tall, and a student: $isStudent');
}
Output:
Setting Up Vim as an IDE for Dart
Dart is great to use with modern IDEs, but if you are utilizing a command-line based text editor to develop Dart programs, let’s say VIM, you can install certain plugins that can enhance your development experience to the next level.
1. Prior to installing any VIM plugin, ensure that you have “vim-plug
” installed. If not, execute the following command to install it:
#For Vim
$ curl -fLo ~/.vim/autoload/plug.vim --create-dirs https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
#For Neovim
$ sh -c 'curl -fLo "${XDG_DATA_HOME:-$HOME/.local/share}"/nvim/site/autoload/plug.vim --create-dirs https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim'
2. Add the following lines to your Vim configuration file (“~/.vimrc
“).
call plug#begin('~/.vim/plugged')
#Dart Plugins
Plug 'dart-lang/dart-vim-plugin'
Plug 'natebosch/vim-lsc'
Plug 'natebosch/vim-lsc-dart'
call plug#end()
3. Open Vim and run the following command to install the plugin:
:PlugInstall
Now, all the Dart syntax will be highlighted with beautiful color in Vim, as shown:
To include line numbers for each line, insert the “set number
” command in your “~/.vimrc
” file.
call plug#begin('~/.vim/plugged')
#Dart Plugins
Plug 'dart-lang/dart-vim-plugin'
Plug 'natebosch/vim-lsc'
Plug 'natebosch/vim-lsc-dart'
#Show Line Numbers
set number
call plug#end()
Save and close the file, and open your Dart program in Vim.
To execute your Dart program within Vim, follow these steps: Open Vim, then replace “location” in the command below with your actual Dart program’s location and execute it.
:term dart /home/linuxtldr/dart_exp2.dart
Output:
Now, let’s end this article here.
Final Word
Note that Dart can also be installed via the Debian package provided by Dart itself. However, the steps to install Dart via the Debian package are intentionally skipped as they require more manual effort, and updating is quite troubling.
Apart from that, if you have any questions or queries related to this article, then feel free to tell us in the comment section.
Till then, peace!
Join The Conversation
Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.