linuxflutterdartamazon-ec2serverpod

Update to latest dart version in aws ec2 linux instance


I am using Serverpod and terraform scripts to upload to aws server. In my pubspec.yaml file somehow I have to have dart SDK version 3.5.0(latest one). But after deploying to aws ec2 linux, I am getting 502 Bad gateway. It says: The current Dart SDK version is 3.1.0(this is on my ec2 linux machine). Because _server requires SDK version 3.5.0, version solving failed. I tried this:

Install Dart

echo "Installing dart"

wget -q https://storage.googleapis.com/dart-archive/channels/stable/release/3.5.0/sdk/dartsdk-linux-x64-release.zip

unzip -q dartsdk-linux-x64-release.zip


sudo mv dart-sdk/ /usr/lib/dart/


sudo chmod -R 755 /usr/lib/dart/

echo 'export PATH="$PATH:/usr/lib/dart/bin"' >> /etc/profile.d/script.sh

It failed saying

cannot move dart-sdk to /usr/lib/dart/ as file already exist.

If I try to rm the dart-sdk inside usr/lib/dart/ it says

can't delete the directory

. And after all it still shows the dart version of my ec2 linux as 3.1.0 which I want to have 3.5.0.


Solution

  • It says "can't delete the directory" because you need add -r and optionally -f flags to rm command.

    from docs:

           -r, -R, --recursive
                  remove directories and their contents recursively
    
           -f, --force
                  ignore nonexistent files and arguments, never prompt
    

    So your command should look pretty much like this:

    sudo rm -rf /usr/lib/dart/dart-sdk
    

    After that rm command, you should repeat another steps, starting from sudo mv dart-sdk/ /usr/lib/dart/

    Also, I personally think that this way of managing Dart versions in not very convenient. Here is recommended way to install/manage Dart SDK versions with package manager: https://dart.dev/get-dart#install

    UPDATED

    While investigate situation in comments me and OP found couple of other problems which were not addressed in this answer, so I update it accordingly:

    1. OP had dart SDK installed already, but in another location (/usr/lib/dart/bin). That's why he always got 3.1.0 version even if deleting directory in /usr/lib/dart/dart-sdk.

    2. To mitigate 1, we decided to set PATH in a way that desired SDK will be earlier in path and dart will take it.

    3. To update PATH, this command worked: echo 'export PATH="/usr/lib/dart/dart-sdk/bin:$PATH"' | sudo tee -a /etc/profile.d/script.sh. Here we set path to desired dart sdk as first entry. And, it's important, sudo doesn't work output redirection (>>) straight away, so we used sudo tee with pipe.